package StackAndQueue;

import java.util.LinkedList;

public class p150逆波兰表达式求值 {
    class Solution {
        public int evalRPN(String[] tokens) {
            LinkedList<Integer> stack = new LinkedList<>();
            for (String token : tokens) {
                if (token.equals("+") || token.equals("-") || token.equals("*") || token.equals("/")) {
                    int num1 = stack.pop();
                    int num2 = stack.pop();
                    switch (token) {
                        case "+": {
                            int ans = num2 + num1;
                            stack.push(ans);
                            break;
                        }
                        case "-": {
                            int ans = num2 - num1;
                            stack.push(ans);
                            break;
                        }
                        case "*": {
                            int ans = num2 * num1;
                            stack.push(ans);
                            break;
                        }
                        default: {
                            int ans = num2 / num1;
                            stack.push(ans);
                            break;
                        }
                    }
                } else {
                    stack.push(Integer.valueOf(token));
                }
            }
            return stack.pop();
        }
    }
}
